package com.lecture.java8.excel.internationalization;

import com.alibaba.excel.context.AnalysisContext;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class ExcelService {

    @Bean
    public ApplicationRunner myRunner(MessageSource messageSource) {
        return args -> {

            InputStream inputStream = new FileInputStream("D:\\用户.xlsx");
            Locale locale = new Locale("zh-CN");
            List<User> userList = new ArrayList<>();
            EasyExcelI18n.read(inputStream, User.class, new I18nAnalysisListener<User>(messageSource, locale, User.class) {
                @Override
                public void invoke(User user, AnalysisContext context) {
                    userList.add(user);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {

                }
            }).sheet().doRead();

            System.out.println(userList);
        };
    }
}
